package com.sky.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    // 当beanFactory初始化完成后，会调用该方法,注册一些beanDefinition
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // 注册一个teacher的beanDefinition
        GenericBeanDefinition gbdf = new GenericBeanDefinition(); // 创建GenericBeanDefinition对象
        gbdf.setBeanClass(Teacher.class); // 设置Bean的Class

        //
        DefaultListableBeanFactory dbf = (DefaultListableBeanFactory) beanFactory; // 转换为DefaultListableBeanFactory对象
        dbf.registerBeanDefinition("teacher", gbdf); // 注册BeanDefinition

    }
}
